package com.yunlian.machine.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yunlian.channel.domain.TbChannel;
import com.yunlian.channel.mapper.TbChannelMapper;
import com.yunlian.common.utils.DateUtils;
import com.yunlian.node.domain.TbNode;
import com.yunlian.node.mapper.TbNodeMapper;
import com.yunlian.type.domain.VmType;
import com.yunlian.type.mapper.VmTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunlian.machine.mapper.VendingMachineMapper;
import com.yunlian.machine.domain.VendingMachine;
import com.yunlian.machine.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author yunlian
 * @date 2024-09-26
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private TbChannelMapper channelMapper;

    @Autowired
    private VmTypeMapper typeMapper;

    @Autowired
    private TbNodeMapper nodeMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //设置货道最大容量
        VmType vmType = typeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //根据点位信息设置详细地址
        TbNode tbNode = nodeMapper.selectTbNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(tbNode.getAddress());
        vendingMachine.setPartnerId(tbNode.getPartnerId());
        vendingMachine.setBusinessType(tbNode.getBusinessType());
        vendingMachine.setRegionId(tbNode.getRegionId());
        //创建时间和更新时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int res = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //设置货道信息
        List<TbChannel> channelList = new ArrayList<>();
        for(int i=1;i<=vmType.getVmRow();i++){
            for(int j=1;j<=vmType.getVmCol();j++){
                TbChannel channel = new TbChannel();
                channel.setChannelCode(i + "-" +j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vendingMachine.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        channelMapper.batchInsertChannel(channelList);
        return res;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        TbNode tbNode = nodeMapper.selectTbNodeById(vendingMachine.getNodeId());
        vendingMachine.setPartnerId(tbNode.getPartnerId());
        vendingMachine.setAddr(tbNode.getAddress());
        vendingMachine.setRegionId(tbNode.getRegionId());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public List<VendingMachine> selectVendingMachineListByNodeId(Long nodeId) {
        return vendingMachineMapper.selectVendingMachineListByNodeId(nodeId);
    }
}
